package com.compty.paint.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.paint.manager.GalleryManager;
import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * 画廊
 * 
 * @author zhuqh
 */
@Entity
@Table(name = "t_gallery")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,include="non-lazy")
public class Gallery implements Serializable {

	private static final long serialVersionUID = 2495234878265158384L;

	private Long id;

	/************ 画廊名称 ****************/
	private String name;

	/************ 省份 ****************/
	private ProvinceInfo provinceInfo;

	/************ 城市 ****************/
	private CityInfo cityInfo;
	
	/************ 详细地址 ****************/
	private String address;

	/************ LOGO 图片****************/
	private ImageInfo imageInfo;
	
	/************ 简介 ****************/
	private String description;
	
	/************ 联系人 ****************/
	private String contact;
	
	/************ 联系电话 ****************/
	private String tel;

	/************ 上传状态 ****************/
	private UploadState uploadState;
	
	/************ 画廊作品集 ****************/
	private List<Painting> paintings = new ArrayList<Painting>();

	/************ 审核状态 ****************/
	private AuditState auditState;

	/************ 上传时间 ****************/
	private Date uploadTime;

	/************ 最后修改时间 ****************/
	private Date modifyTime;

	/************ 录入人 ****************/
	private User entryPerson;
	
	@Transient
	private Integer totalCount_painting = 0; //作品数量
	
	@Transient
	private Integer totalCount_sold = 0; //已售数量
	
	@Transient
	private Integer totalCount_painters = 0; //画家数量
	
	@Transient
	private Integer totalCount_visit = 0; //访问次数
	
	private String imagepath;//图片路径

	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name="name",length=80)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="province_id")
	public ProvinceInfo getProvinceInfo() {
		return provinceInfo;
	}

	public void setProvinceInfo(ProvinceInfo provinceInfo) {
		this.provinceInfo = provinceInfo;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="city_id")
	public CityInfo getCityInfo() {
		return cityInfo;
	}

	public void setCityInfo(CityInfo cityInfo) {
		this.cityInfo = cityInfo;
	}

	@Column(name = "address",length = 200)
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="image_id")
	public ImageInfo getImageInfo() {
		return imageInfo;
	}

	public void setImageInfo(ImageInfo imageInfo) {
		this.imageInfo = imageInfo;
	}

	@Column(name = "description",length=2000)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name = "contact",length = 40)
	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	@Column(name = "tel",length = 20)
	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="uploadstate_id")
	public UploadState getUploadState() {
		return uploadState;
	}

	public void setUploadState(UploadState uploadState) {
		this.uploadState = uploadState;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="auditstate_id")
	public AuditState getAuditState() {
		return auditState;
	}

	public void setAuditState(AuditState auditState) {
		this.auditState = auditState;
	}

	@Column(name = "uploadtime")
	public Date getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	@Column(name = "modifytime")
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="userinfo_id")
	public User getEntryPerson() {
		return entryPerson;
	}

	public void setEntryPerson(User entryPerson) {
		this.entryPerson = entryPerson;
	}

	 @ManyToMany(cascade =  {CascadeType.REMOVE}
	    , fetch = FetchType.LAZY)
	    @JoinTable(name = "T_gallery_painting", joinColumns =  {
	        @JoinColumn(name = "gallery_id")
	    }
	    , inverseJoinColumns =  {
	        @JoinColumn(name = "painting_id")
	    }
	    )
	public List<Painting> getPaintings() {
		return paintings;
	}

	public void setPaintings(List<Painting> paintings) {
		this.paintings = paintings;
	}

	@Transient
	public Integer getTotalCount_painting() {
		
		totalCount_painting = 0;
		
		if(this.paintings != null){
			totalCount_painting = this.paintings.size();
		}
		return totalCount_painting;
	}

	public void setTotalCount_painting(Integer totalCountPainting) {
		totalCount_painting = totalCountPainting;
	}

	@Transient
	public Integer getTotalCount_sold() {
		if(this.paintings != null){
//			for (Painting painting : paintings) {
//				if("2".equals(painting.getSoldFlag())){
//					totalCount_sold++;
//				}
//			}
		}
		return totalCount_sold;
	}

	public void setTotalCount_sold(Integer totalCountSold) {
		totalCount_sold = totalCountSold;
	}

	@Transient
	public Integer getTotalCount_painters() {
		
		GalleryManager galleryManager  = SpringContextHolder.getBean("galleryManager");
		List<Painter>  painters = galleryManager.findPaintersByGalleryId(this.id);
		if(painters != null){
			totalCount_painters = painters.size();
		}
		
		return totalCount_painters;
	}

	public void setTotalCount_painters(Integer totalCountPainters) {
		totalCount_painters = totalCountPainters;
	}

	@Transient
	public Integer getTotalCount_visit() {
		return totalCount_visit;
	}

	public void setTotalCount_visit(Integer totalCountVisit) {
		totalCount_visit = totalCountVisit;
	}
	
	@Transient
	public String getImagepath() {
		if( imageInfo!= null ){
			this.imagepath = Constants.IMAGE_SERVER+Constants.ExhibitionFlag+"/"+imageInfo.getId()+"."+imageInfo.getSuffix();
		}
		else{
			this.imagepath = Constants.IMAGE_SERVER ;
		}
		return imagepath;
	}
	
}
